package com.bosssoft.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibAccountDTO;
import com.bosssoft.entity.po.FibAccountPO;
import com.bosssoft.entity.vo.FibAccountVO;
import com.bosssoft.exception.DaoException;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 账户表(FibAccount)表控制层
 *
 * @author makejava
 * @since 2021-08-17 15:08:07
 */
@Slf4j
@RestController
@ResponseBody
@RequestMapping("/fibAccount")
public class FibAccountController {
    /**
     * 服务对象
     */
    @Resource
    private FibAccountService fibAccountService;

        /**
         * 查询指定id
         *
         * @return
         */
    @PostMapping("/select")
    public Result select(@Validated @RequestBody Long id) {
        try {
            FibAccountDTO records = fibAccountService.selectOneById(id);
            System.out.println(records);
            return Result.ok().message("查询成功").data("list",records);
        } catch (Exception e) {
            return Result.error().code(1).message("查询失败");
        } finally {
        }
    }
    /**
     * 查询所有信息
     *
     * @return
     */
    @PostMapping("/selectAll")
    public Result selectAll(@RequestParam(defaultValue = "1") Integer pageNo,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            FibAccountVO fibAccountVO) {
        try {
            FibAccountPO fibAccountPO = new FibAccountPO();
            BeanUtils.copyProperties(fibAccountVO,fibAccountPO);
            Page<FibAccountPO> page = new Page<>(pageNo,pageSize);
            QueryWrapper<FibAccountPO> fibAccountVOQueryWrapper = new QueryWrapper<>();
            PageResult<FibAccountPO> pageList = fibAccountService.page(page, fibAccountPO);
            return Result.ok().data("page",pageList);
        } catch (DaoException e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //@PostMapping("/selectAll")
    //public Result selectAll(@RequestBody Long page,Long limit) {
    //    try {
    //        List<FibAccountDTO> records = fibAccountService.selectAll(page,limit);
    //        return Result.ok().message("查询成功").data("list",records);
    //    } catch (Exception e) {
    //        return Result.error().code(1).message("查询失败");
    //    } finally {
    //    }
    //}
    /**
     * 根据id删除
     *
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@Validated @RequestBody FibAccountVO fibAccountVO) {
        FibAccountDTO fibAccountDTO = new FibAccountDTO();
        BeanUtils.copyProperties(fibAccountVO, fibAccountDTO);
        Long accId = fibAccountDTO.getAccId();
        boolean delete = fibAccountService.delete(accId);
        if (delete) {
            return Result.ok().code(1).message("删除账户成功");
        }else {
            return Result.error().code(2).message("删除账户失败");
        }
    }

    /**插入一条数据
     * @return*/
    @PostMapping("/insert")
    public Result insert(@Validated @RequestBody FibAccountVO fibAccountVO) {
        FibAccountDTO fibAccountDTO = new FibAccountDTO();
        BeanUtils.copyProperties(fibAccountVO, fibAccountDTO);
        boolean insert = fibAccountService.insert(fibAccountDTO);
        if (insert) {
            return Result.ok().code(1).message("添加账户成功");
        }else {
            return Result.error().code(2).message("添加账户失败");
        }
    }
    /**更新数据
     * @return*/
    @PostMapping("/update")
    public Result update(@Validated @RequestBody FibAccountVO fibAccountVO) {
        FibAccountDTO fibAccountDTO = new FibAccountDTO();
        BeanUtils.copyProperties(fibAccountVO, fibAccountDTO);
        boolean update = fibAccountService.update(fibAccountDTO);
        if (update) {
            return Result.ok().code(1).message("更新账户成功");
        }else {
            return Result.error().code(2).message("更新账户失败");
        }
    }
    /**
     * 更新激活状态
     * @return
     *
     */
    @PostMapping("/updateDeleted")
    public Result updateDeleted(@Validated @RequestBody FibAccountVO fibAccountVO){
        FibAccountDTO fibAccountDTO = new FibAccountDTO();
        BeanUtils.copyProperties(fibAccountVO, fibAccountDTO);
        boolean update = fibAccountService.updateDeleted(fibAccountDTO);
        if (update) {
            return Result.ok().code(1).message("更新账户激活状态成功");
        }else {
            return Result.error().code(2).message("更新账户激活状态失败");
        }
    }
    /**更新密码
     * @return*/
    @PostMapping("/updatePassword")
    public Result updatePassword(@Validated @RequestBody FibAccountVO fibAccountVO){
        FibAccountDTO fibAccountDTO = new FibAccountDTO();
        BeanUtils.copyProperties(fibAccountVO, fibAccountDTO);
        boolean update = fibAccountService.updatePassword(fibAccountDTO);
        if (update) {
            return Result.ok().code(1).message("更新账户密码成功");
        }else {
            return Result.error().code(2).message("更新账户密码失败");
        }
    }
    /**更新余额
     * @return
     * */
    @PostMapping("/updateBalance")
    public Result updateBalance(@Validated @RequestBody FibAccountVO fibAccountVO){
        FibAccountDTO fibAccountDTO = new FibAccountDTO();
        BeanUtils.copyProperties(fibAccountVO, fibAccountDTO);
        boolean update = fibAccountService.updateBalance(fibAccountDTO);
        if (update) {
            return Result.ok().code(1).message("更新账户余额成功");
        }else {
            return Result.error().code(2).message("更新账户余额失败，余额不足或账户未激活");
        }
    }
}